package com.msb.servlet;

import com.msb.entity.User;
import com.msb.mapper.UserMapper;
import com.msb.unitil.DBUnitil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @version : 1.0
 * @Author: yangmingsong
 * @Date: 2021/2/7-02-07-23:33
 * @Description: demon
 */
@WebServlet(urlPatterns = "/Douser")
public class Douser extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

//获取表单数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username == null || username.length() < 0) {
                request.setAttribute("msg", "数据类型有误");
                return;
        }
        if (password == null || password.length() < 0) {
            request.setAttribute("msg", "数据类型有误");
            return;
        }
//调用DBUtils方法
        UserMapper mapper = DBUnitil.getsqlsession().getMapper(UserMapper.class);
        User user1 = mapper.select1(password);
        User user2 = mapper.select2(username);
        DBUnitil.getsqlsession().close();
        request.setAttribute("user1",user1);
        request.setAttribute("user2",user2);
        if(user1==null&&user2==null)
        {

            request.setAttribute("msg2","您尚未注册，请您注册");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else if(user1==null||user2==null)
        {
            request.setAttribute("msg2", "您输入的用户名或者密码不正确");
            request.getRequestDispatcher("login.jsp").forward(request,response);

        }else if((user1!=null)&&(user2!=null)&&(user2.getPassword()).equals(user1.getPassword())&&(user1.getUsername()).equals(user2.getUsername()))
        {
            response.sendRedirect("home.jsp");

        }else
        {
            request.setAttribute("msg2","您输入的用户名或者密码不正确");
            request.getRequestDispatcher("login.jsp").forward(request,response);

        }

    }
}
